﻿// As agreed, all these functions and constants are public, but not documented yet.

package gui.hug;

  import flash.display.DisplayObject;
  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.text.TextFieldType;
  import flash.text.TextFieldAutoSize;
  import flash.text.TextFormat;
  import flash.text.TextFormatAlign;
  import flash.display.LineScaleMode;
  import flash.display.CapsStyle;
  import flash.display.JointStyle;
  import org.flashdevelop.utils.FlashConnect;


  /**
  * @author Alexey Kharkov
  * @modified by Natalia Ukhorskaya
  */
class Utils
  {	
	public static var fnm:String = "Tahoma";

    // VKontakte standard colors
    public static inline var BK_COL:Int = 0xF7F7F7;
    
    public static inline var BLUE_BK_COL:Int = 0xDAE1E8;
    public static inline var BLUE_TXT_COL:Int = 0x36638e;

    public static inline var BORDER_COL:Int = 0xd8dfea;
    public static inline var BORDER2_COL:Int = 0xc0cad5;
    
    public static inline var ARROW_BG_COL:Int = 0xe1e8ed;
    public static inline var ARROW_BG_BORDER_COL:Int = 0xd2dbe0;
    
    public static inline var SEL_BG_COL:Int = 0x5f83a5;
    public static inline var SEL_BG_BORDER_COL:Int = 0x345f88;

    public static inline var BUT1_TXT_COL:Int = 0x2B587A;

    // addText params
    public static inline var TXT_UNDERLINE:Int = 0x0001;
    public static inline var TXT_BOLD:Int = 0x0002;
    public static inline var TXT_MULTILINE:Int = 0x0004;
    public static inline var TXT_CENTER:Int = 0x0008;
    public static inline var TXT_AUTOSIZE:Int = 0x0010;
    public static inline var TXT_INPUT:Int = 0x0020;
    public static inline var TXT_BORDER:Int = 0x0040;
    public static inline var TXT_HTML:Int = 0x0080;
    
	public function new () {
	
	}
	
    // Rectangles
    public static function fillRect( obj:Dynamic, x:Int, y:UInt, w:Float, h:Float, c:UInt, alpha:Float = 1.0 )
    { 
      obj.graphics.lineStyle();
      obj.graphics.beginFill( c, alpha );
      obj.graphics.drawRect( x, y, w, h );
      obj.graphics.endFill();
    }
    
    public static function rect( obj:Dynamic, x:Int, y:Int, w:Float, h:Float, c:UInt, bc:Int, alpha:Float = 1.0 )
    {
      fillRect( obj, x, y, w, h, c, alpha );
      hollowRect( obj, x, y, w, h, bc );
    }
    
    public static function hollowRect( obj:Dynamic, x:Int, y:Int, w:Float, h:Float, c:UInt )
    {
      obj.graphics.lineStyle( 1, c, 1.0, true, LineScaleMode.NORMAL, CapsStyle.SQUARE, JointStyle.MITER, 1.414 );
      obj.graphics.drawRect( x, y, w, h );
    }

    // Round rectangles
    public static function fillRRect( obj:Dynamic, x:Int, y:Int, w:Float, h:Float, c:Int, r:Int )
    {
      obj.graphics.lineStyle();
      obj.graphics.beginFill( c, 1.0 );
      obj.graphics.drawRoundRect( x, y, w, h, r );
      obj.graphics.endFill();
    }

    // Lines
    public static function line( obj:Dynamic, x1:Float, y1:Float, x2:Float, y2:Float, c:Float, alpha:Float = 1.0 )
    {
      obj.graphics.lineStyle( 1, c, alpha, true, LineScaleMode.NORMAL, CapsStyle.SQUARE, JointStyle.MITER, 1.414 );
      obj.graphics.moveTo( x1, y1 );
      obj.graphics.lineTo( x2, y2 );
    }
    
    public static function horLine( obj:Dynamic, x1:Float, x2:Float, y:Float, c:Float, alpha:Float = 1.0 )
    {
      line( obj, x1, y, x2, y, c, alpha );
    }

    public static function vertLine( obj:Dynamic, x:Float, y1:Float, y2:Float, c:Float, alpha:Float = 1.0 )
    {
      line( obj, x, y1, x, y2, c, alpha );
    }

    public static function vertSeparator( obj:Dynamic, x:Int = 0, y:Int = 0 )
    {
      vertLine( obj, x    , y, y + 10, 0xe9be90 );
      vertLine( obj, x + 1, y, y + 10, 0xa7d4ff );
    }
    
    // Dash Lines
    public static function horDashLine( obj:Dynamic, x1:Float, x2:Float, y:Float, c:Int, step:UInt = 4 ) // Should be x2 > x1!
    {
      obj.graphics.lineStyle( 1, c );
      var x: Float = x1;
	  while (x < x2)
      {
        obj.graphics.moveTo( x, y );
        obj.graphics.lineTo( Math.min(x2 + 1, x + step), y );
		x += 2 * step;
      }
    }
    
    public static function vertDashLine( obj:Dynamic, x:Float, y1:Float, y2:Float, c:Int, step:UInt = 4 ) // Should be y2 > y1!
    {
      obj.graphics.lineStyle( 1, c );
      var y: Float = y1;
	  while (y < y2)
      {
        obj.graphics.moveTo( x, y );
        obj.graphics.lineTo( x, Math.min(y2 + 1, y + step) );
		y += 2 * step;
      }
    }

    // Dash rects
    public static function dashRect( obj:Dynamic, x:Int, y:Int, w:Float, h:Float, c:Int, step:UInt = 4 )
    {
      horDashLine( obj, x, x + w, y    , c, step );
      horDashLine( obj, x, x + w, y + h, c, step );
      
      vertDashLine( obj, x    , y, y + h, c, step );
      vertDashLine( obj, x + w, y, y + h, c, step );
    }
    
    // TextFields
    public static function getTxtFormat( fsz:Int, prm:Int = 0 ):TextFormat
    {
      var f:TextFormat = new TextFormat();
      f.font = fnm;
      f.size = fsz;
      f.underline = isPar(prm, TXT_UNDERLINE);
      f.bold = isPar(prm, TXT_BOLD);
      f.align = isPar(prm, TXT_CENTER) ? TextFormatAlign.CENTER : TextFormatAlign.LEFT;
      return f;
    }
    
    public static function setText( tf:TextField, s:String, isHtml:Bool = false )
    {
      var f:TextFormat = tf.getTextFormat();
      
      if ( isHtml )
        tf.htmlText = s;
      else
        tf.text = s;
        
      tf.setTextFormat( f );
      updSz( tf );
    }
    
    public static function updSz( tf:TextField, hh:UInt = 0, ww:UInt = 0 )
    {
      tf.autoSize = TextFieldAutoSize.LEFT;
      var h:Float = tf.height;
      var w:Float = tf.width;
      tf.autoSize = TextFieldAutoSize.NONE;
      tf.height = (hh > 0  &&  hh < h) ? hh : h;
      tf.width  = (ww > 0  &&  ww < w) ? ww : w;
    }
    
    public static function addBorder( tf:TextField )
    {
      tf.background = true;
      tf.border = true;
      tf.borderColor = BORDER2_COL;
    }
    
    // To be refactored...
    public static function addText( x:Int, y:Int, w:Float, fsz:Int, s:String, tc:Int = 0, prm:Int = 0, h_:Float = 0 ):TextField
    {
      var f:TextFormat = getTxtFormat( fsz, prm );
      
      var tf:TextField = new TextField();
      tf.defaultTextFormat = f;
      tf.setTextFormat( f );
      tf.x = x;
      tf.y = y;
      tf.width = w;

      if ( s == null )
        tf.text = "";
      else
      {
        if ( isPar(prm, TXT_HTML) )
          tf.htmlText = s;
        else
          tf.text = s;
      }
      
      if ( isPar(prm, TXT_MULTILINE) )
      {
        tf.wordWrap = true;
        tf.multiline = true;
      }

      if ( isPar(prm, TXT_AUTOSIZE)  ||  w == 0 )
        tf.autoSize = TextFieldAutoSize.LEFT;
        
      if ( isPar(prm, TXT_INPUT) )
      {
        tf.selectable = true;
        tf.mouseEnabled = true;
        tf.type = TextFieldType.INPUT;
      } else
      {
        //!!tf.selectable = false;
      }
      
      if ( isPar(prm, TXT_BORDER) )
      {
        addBorder( tf );
      }
      
      var h:Float = (fsz * 1.6); // Magic constant...
      tf.height = (h_ == 0)  ?  h  :  h_;
      
      if ( isPar(prm, TXT_AUTOSIZE) )
      {
        h = tf.height;
        w = (w == 0) ? tf.width : Math.min( w, tf.width );
        tf.autoSize = TextFieldAutoSize.NONE;
        tf.height = h;
        tf.width = w;// + 2;
        if ( h_ > 0  &&  h_ < h )
          tf.height = h_;
      }
      
      tf.textColor = tc;
      
      if ( s != null  &&  isPar(prm, TXT_HTML) )
        tf.htmlText = s;

      return tf;
    }
    
    // ----------------------------------------------------------------- Internal methods.
    public static function drawArrow( obj: Dynamic, x:Int, y:Int, down:Bool, c:Int = 0 )
    {
      var dy:UInt = down ? 1 : -1;

      Utils.horLine ( obj, x + 0, x + 6, y, c );
      Utils.horLine ( obj, x + 1, x + 5, y + dy, c );
      Utils.horLine ( obj, x + 2, x + 4, y + 2*dy, c );
      
      Utils.vertLine( obj, x + 3, y, y + 3*dy, c );
    }
    
	//WARNING
    public static function topParent( obj:DisplayObject ):DisplayObject
    /*{
      return (obj.parent as DisplayObject) 
        ? topParent( (obj.parent as DisplayObject) ) 
        : (obj as DisplayObject);
    }
	if (obj.parent!=null) {
				return topParent(obj.parent);
			}
			else {
				return obj;
			}*/
	{
		return obj.root;
		
	}

    // ----------------------------------------------------------------- Private mehods.
    private static function isPar( p:Int, pp:Int ):Bool
    {
      return (p & pp) != 0;
    }
  }
